// -*- c++ -*-
/**
  \file SS_Globals.pix
  \author Morgan McGuire, http://graphics.cs.williams.edu

  For use with G3D::SuperShader.
  This file is included into SS_NonShadowedPass.pix and SS_ShadowMappedLightPass.pix.

  \edited  2010-02-25
  \created 2007-12-18
 */

/** Surface position in world space */
varying vec3        wsPosition;

#ifndef NO_LIGHT
/** World space light position */
uniform vec4        lightPosition0;

/** Power of the light */
uniform vec3        lightColor0;

/** Spot light facing direction (unit length) */
uniform vec3        lightDirection0;

/** w element is the spotlight cutoff angle*/
uniform vec4        lightAttenuation0;
#endif

#ifdef CUSTOMCONSTANT
    uniform vec4        customConstant;
#endif

#ifdef CUSTOMMAP
    uniform sampler2D   customMap;
#endif

// Material
#ifdef SPECULARCONSTANT
    uniform vec4        specularConstant;
#endif

#ifdef SPECULARMAP
    uniform sampler2D   specularMap;
#endif

#ifdef LAMBERTIANCONSTANT
    uniform vec4        lambertianConstant;
#endif

#ifdef LAMBERTIANMAP
    uniform sampler2D   lambertianMap;
#endif

/** Set to -1 to flip the normal. */
uniform float       backside;

varying vec2        texCoord;
varying vec3        tan_Z;

#ifdef NORMALBUMPMAP
#   if (PARALLAXSTEPS > 0)
        /** Multiplier for bump map.  Typically on the range [0, 0.05]
          This increases with texture scale and bump height. */
        uniform float       bumpMapScale;

        /** How high is the 0-level of the bump map? On the range [0, 1] */
        uniform float       bumpMapBias;

        /** Un-normalized (interpolated) tangent space eye vector */
        varying vec3        _tsE;
#   endif    
    /** xyz = normal, w = bump height */
    uniform sampler2D   normalBumpMap;
    varying vec3        tan_X, tan_Y;
#endif


/** Computes x<sup>5</sup> */
float pow5(in float x) {
    float x2 = x * x;
    return x2 * x2 * x;
}

/** Compute Schlick's approximation of the Fresnel coefficient.  The
    original goes to 1.0 at glancing angles, which makes objects
    appear to glow, so we clamp it.

    We never put a Fresnel term on perfectly diffuse surfaces, so if
    F0 is exactly black we keep the result black.
    */
// Must match GLG3D/SuperBSDF.h
vec3 computeF(in vec3 F0, in float cos_i) {
    return (F0.r + F0.g + F0.b > 0.0) ? (mix(F0, vec3(1.0), clamp(pow5(1.0 - cos_i), 0.0, 0.3))) : F0;
}

float square(in float x) {
    return x * x;
}

/** Matches SuperBSDF::unpackSpecularExponent*/
float unpackSpecularExponent(in float e) {
    return square((e * 255.0 - 1.0) * (1.0 / 253.0)) * 1024.0 + 1.0;
}
/* End SS_Globals.pix */

